package com.qubuyer.core.app

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ProgressBar
import androidx.annotation.LayoutRes
import androidx.fragment.app.Fragment
import com.hjq.toast.ToastUtils
import com.qubuyer.core.ext.dp
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import org.greenrobot.eventbus.EventBus
import kotlin.coroutines.CoroutineContext

/**
 * Author: linwg
 * Time: 2021/2/2 0002
 * Description:
 */
abstract class BaseFragment : Fragment(), IBaseView, CoroutineScope {
    lateinit var mContext: BaseActivity
    var preFragment: BaseFragment? = null
    lateinit var mRoot: View
    var hasLazyLoad = false

    @get:LayoutRes
    abstract val layoutId: Int
    override val coroutineContext: CoroutineContext
        get() = Dispatchers.Main + Job()
    private val globalLoadingView: Lazy<View> = lazy {
        customLoadingView()
    }

    open fun getTitle(): String {
        return ""
    }

    open fun onBackPressed(): Boolean {
        return false
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is BaseActivity) {
            mContext = context
        } else {
            throw IllegalStateException("BaseFragment can be use in BaseActivity only.")
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (!EventBus.getDefault().isRegistered(this)) {
            try {
                EventBus.getDefault().register(this)
            } catch (e: Exception) {
            }
        }
    }

    override fun onResume() {
        super.onResume()
        if (!hasLazyLoad || needLazyLoad()) {
            hasLazyLoad = true
            lazyLoad()
        }
    }

    open fun needLazyLoad(): Boolean {
        return false
    }

    open fun lazyLoad() {
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun onDestroyView() {
        cancel()
        super.onDestroyView()
    }

    override fun toast(msg: CharSequence) {
        ToastUtils.show(msg)
    }

    override fun toast(msgRes: Int) {
        ToastUtils.show(msgRes)
    }

    override fun showProgressDialog(hint: String?) {
        mContext.showProgressDialog(hint)
    }

    override fun hideProgressDialog() {
        mContext.hideProgressDialog()
    }

    open fun customLoadingView(): View {
        return FrameLayout(mContext).apply {
            setBackgroundColor(Color.WHITE)
            isClickable = true
            addView(ProgressBar(mContext).apply {
                layoutParams = FrameLayout.LayoutParams(36.dp, 36.dp, Gravity.CENTER)
            })
        }
    }

    override fun showPageLoadingView() {
        if (this::mRoot.isInitialized) {
            val parent = mRoot.parent
            if (parent is ViewGroup) {
                parent.addView(globalLoadingView.value)
            }
        }
    }

    override fun hidePageLoadingView() {
        if (this::mRoot.isInitialized) {
            val parent = mRoot.parent
            if (parent is ViewGroup) {
                parent.removeView(globalLoadingView.value)
            }
        }
    }

    /***
     * 收起软键盘
     */
    fun hideKeyBord() {
        mContext.hideKeyBord()
    }

    /***
     * 打开软键盘
     */
    fun showKeyBord() {
        mContext.showKeyBord()
    }

    override fun processNetworkInvalid() {
        mContext.processNetworkInvalid()
    }

    override fun processTokenInvalid() {
        mContext.processTokenInvalid()
    }

    inline fun <reified T : Activity> startActivity(clazz: Class<T> = T::class.java) {
        startActivity(Intent(mContext, clazz))
    }
}